<div class="sf_admin_form_row">
	<label>Дополнительные изображения.</label>
	<?php if($form->getObject()->getId()):?>
		<?php use_helper('jQuery');?>
		<div id="ajax_loader" style="display:none;"><?php echo image_tag('ajax-loader.gif')?></div>
		<div id="img_upload_button">Добавить изображение</div>
		<ul id="additional_images">
		<?php	$images = $form->getObject()->getAdditionalImages(); 
				if( !empty($images) ): ?>
					<?php foreach($images as $image):?>
						<li id='image<?php echo $image['id']?>'><img src='/uploads/news/thumbnail_90_90/<?php echo $image['image']?>'/>
							<a id='remove_link' onclick='if(confirm("Вы уверены?")){deleteImage(<?php echo $image['id']?>)}' href='javascript://' title='Удалить изображение'>Удалить</a>
						</li>
					<?php endforeach;?>
				<?php endif;?>
		</ul>
	<?php else:?>
		<span style="color:#369">Сохраните для добавления дополнительных фото</span>
	<?php endif;?>
<br style="clear:both;" />
</div>

<script type="text/javascript">
$(document).ready(function() {
	new AjaxUpload('img_upload_button', {
		  // Location of the server-side upload script
		  // NOTE: You are not allowed to upload files to another domain
		  action: '/backend.php/news/upload',
		  // File upload name
		  name: 'userfile',
		  // Additional data to send
		  data: {
		    object_id : <?php echo $form->getObject()->getId()?>,
		    object_enity : 'news'
		  },
		  // Submit file after selection
		  autoSubmit: true,
		  // The type of data that you're expecting back from the server.
		  // HTML (text) and XML are detected automatically.
		  // Useful when you are using JSON data as a response, set to "json" in that case.
		  // Also set server response type to text/html, otherwise it will not work in IE6
		  responseType: false,
		  // Fired after the file is selected
		  // Useful when autoSubmit is disabled
		  // You can return false to cancel upload
		  // @param file basename of uploaded file
		  // @param extension of that file
		  onChange: function(file, extension){$('#ajax_loader').show()},
		  // Fired before the file is uploaded
		  // You can return false to cancel upload
		  // @param file basename of uploaded file
		  // @param extension of that file
		  onSubmit: function(file, extension) {},
		  // Fired when file upload is completed
		  // WARNING! DO NOT USE "FALSE" STRING AS A RESPONSE!
		  // @param file basename of uploaded file
		  // @param response server response
		  onComplete: function(file, response) {
			  	$('#ajax_loader').hide();
			  	var obj = eval("(" + response + ")" );
				if (obj.imageid) {
					var el = "<li id='image"+obj.imageid+"'><img src='/uploads/news/thumbnail_90_90/" + obj.filename + "'/>" +
							 "<a id='remove_link' onclick='if(confirm(\"Вы уверены?\")){deleteImage("+obj.imageid+")}' href='javascript://' title='Удалить изображение'>Удалить</a></li>";
					$('#additional_images').append(el);
					alert('Файл ['+file+'] загружен успешно');
				} else {
					alert(obj.error);
				}
			}
		});
});
function deleteImage(image_id)
{
	$.ajax({
		type: "POST",
		url: "/backend.php/news/remove_image",
		data: "image_id="+image_id,
		beforeSend: function(){ $('#ajax_loader').show(); },
		success: function(msg){
				if(msg=='success') {
					$('#ajax_loader').hide();
					$('#image'+image_id).remove();
				} else {
					alert(msg);
				}
			}
		});
}
</script>